// 工具类
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:psi/entity/response/login_resp.dart';
import 'package:psi/routes/app_pages.dart';
import 'package:psi/services/share_preference_service.dart';
import 'package:image_picker/image_picker.dart';
import 'package:psi/services/user_logic.dart';
import "package:intl/intl.dart" as intl;
import 'package:psi/utils/oss_util.dart';
import 'package:psi/utils/widget_util.dart';
import '../generated/locales.g.dart';
import '../services/log_service.dart';



class Utils {
  Utils._();

  static UserLogic userLogic = Get.find<UserLogic>();

  /// 取消点击事件
  static void clearFocus() {
    FocusManager.instance.primaryFocus?.unfocus();
  }

  /// 显示toast
  static Future<void> showToast(String? msg) {
    return SmartDialog.showToast(msg ?? '', alignment: Alignment.center, displayType: SmartToastType.last);
  }


  /// 展示loading
  static Future<T?> showLoading<T>({String? msg, bool? backDismiss, bool? usePenetrate}) {
    // EasyLoading.show(status: msg);
    return SmartDialog.showLoading<T>(
      msg: msg ?? "请稍等...",
      // maskColor: WidgetUtil.color(Colors.white, colorDarkMode: Colors.black),
      backDismiss: backDismiss,
      usePenetrate: usePenetrate,
    );
  }

  /// debug信息打印
  static void logD(dynamic object) {
    if (!kIsWeb) {
      LogService.logger.d(object);
    }
  }

  /// info信息打印
  static void logI(dynamic object) {
    if (!kIsWeb) {
      LogService.logger.i(object);
    }
  }

  /// error信息打印
  static void logE(Object? message, {dynamic error, StackTrace? stackTrace}) {
    if (!kIsWeb) {
      LogService.to.e(message, error: error, stackTrace: stackTrace);
    }
  }

  /// 取消loading
  static Future<void> hideLoading() {
    // EasyLoading.dismiss();
    return SmartDialog.dismiss(status: SmartStatus.loading);
  }

  /// 秒转化为 1m8s
  static String formatTimeToMS(int milliseconds) {
    final seconds = milliseconds ~/ 1000;
    int min = seconds ~/ 60;
    int sec = seconds % 60;
    return '${min.toString()}m${sec.toString()}s';
  }

  /// 秒转化为 01:08
  static String formatTimeToMSFormat(int milliseconds) {
    final seconds = milliseconds ~/ 1000;
    int hour = seconds ~/ (60 * 60);
    int min = seconds ~/ 60;
    int sec = seconds % 60;
    if (hour > 0) {
      return '${hour.toString().padLeft(2, '0')}:${min.toString().padLeft(2, '0')}:${sec.toString().padLeft(2, '0')}';
    } else {
      return '${min.toString().padLeft(2, '0')}:${sec.toString().padLeft(2, '0')}';
    }
  }

  /// 秒转化为 10′29″
  static String formatTimeToMSMark(int milliseconds) {
    final seconds = milliseconds ~/ 1000;
    int min = seconds ~/ 60;
    int sec = seconds % 60;
    return '${min.toString().padLeft(2, '0')}′${sec.toString().padLeft(2, '0')}″';
  }


  static String? getFormatStepCount({num? amount, int position = 2}) {
    if (amount == null) {
      return null;
    }
    if ((amount < 1000 && amount >= 0) ||( amount < 0 && amount > -1000)) {
      return amount.toStringAsFixed(position);
    }
    String numFormat = "0,000";
    if (position > 0) {
      for (int i = 0; i < position; i ++) {
        if (i == 0) {
          numFormat += ".";
        }
        numFormat += "0";
      }
    }
    var format = intl.NumberFormat(numFormat);
    return format.format(amount);
  }

  static Future<String?> selectImage() async {
    final ImagePicker picker = ImagePicker();
    XFile? image;
    if (kIsWeb) {
      // showToast("暂不支持修改头像");
      image = await picker.pickImage(source: ImageSource.gallery);
    } else {
      final index = await WidgetUtil.showSheet(title: "请选择", items: ["从图库中选择", "拍照"]);
      if (index == null) {
        return null;
      }
      if (index == 0) {
        image = await picker.pickImage(source: ImageSource.gallery);
      } else {
        image = await picker.pickImage(source: ImageSource.camera);
      }
    }
    if (image != null) {
      File file = File(image.path);
      String? urlStr = await OssUtil.uploadFile(file.path);
      return urlStr;
    } else {
      return null;
    }
  }


}
